$(document).ready(function () {
  $('[data-toggle=offcanvas]').click(function () {
    $('.row-offcanvas').toggleClass('active')
  });
  
	  $( "#keyword").keypress(function(e) {
		  if (e.which == 13) {
			  search()
		  }
	  });
	  
	  $('#see-more-songs').click(see_more_songs)
});

$(function () {
	$('#search-btn').click(search);
	init_search_vehicle();
});

function search() {
	var key = $('#keyword').val();
	
	if (! key) {
		return;
	}
	
	window.location.href = "/karaoke/search/?keyword="+ key.split(" ").join("+");
}


function init_search_vehicle() {
  $("#keyword").autocomplete({
      source: function(request, response) {
          var key = $("#keyword").val();
          if (key.length < 2) {
              return;
          }
          console.log(key)
          $('.loading').show();
          
          $.ajax({
              url: '/ma-so-karaoke/tim-kiem.html',
              type: 'get',
              data: 'keyword=' + key,
              success: function(data) {
                  response($.map(eval(data), function(item) {
                	
                	  return {
                          id: item.id,
                          value: item.name,
                          short_cont: item.short_cont,
                          code: item.code,
                      };
                  }));
                  
              }
          });
      }
      ,
      minLength: 1,
      select: function(event, ui) {
          $('.moto').attr('src', '/public/upload/vehicle/' + ui.item.img);
          product_appropriate_for_vehicle(ui.item.cat_id);
      },
      open: function() {
          $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
      },
      close: function() {
          $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
      }

  }).data( "ui-autocomplete" )._renderItem = function(ul, item) {
      console.log(item)
      return $('<li class="search-item">')
          .append( '<a><h2>' + item.code + '</h2><h3>' + item.label + 
          		"</h3><h6>"+ item.short_cont +"<h6></a>")
          .appendTo(ul);
  };
}


function see_more_songs() {
    var see_more = $('#see-more-songs'),
            num_displayed = $('.item').length,
            list_id = 'row-list-'+ num_displayed;

    $('#list-item').append('<div class="clearfix"></div><div id="'+ list_id +'"></div>');
    $('#'+list_id+'').hide();
    see_more.html('Đang tải ...')
    $.ajax({
            url: '/ma-so-karaoke/xem-them.html',
            data: {num_displayed: num_displayed, action: see_more.attr('action'), 
                    keyword: $('#keyword').val(),
                    vol: $('#vol').val()},
            type: 'get',
            success: function (res) {
                var res= eval("("+ res +")");

                $('#'+list_id+'').html(res.data);
                $('#'+list_id+'').slideDown('slow');
                if (! res.see_more_btn) {
                        see_more.hide()
                }
                see_more.html('Xem thêm')
            }
    });
}